package network;

public class TPA {

	/**
	 * The adress that this TPA represents
	 * @invariant 0<= adress <= Network.Constants.TPA_MAX
	 */
	private int adress;
	
	/**
	 * Creates a new TPA from the integer, adress should be in range of TPA
	 * @param adress the integer repesentation of this adress
	 * @requires 0 <= adress <= Network.Constants.TPA_MAX
	 */
	public TPA(int adress){
		if(adress < 0 || adress > Constants.TPA_MAX){
			throw new IllegalArgumentException("adress out of range");
		}
		
		this.adress = adress;
	}
	
	/**
	 * Constructs a new TPA from the byte, adress should be in range of TPA
	 * The meaningfull bits of the adress are expected to be in the right most
	 * bis of the byte.
	 * @param adress
	 * @requires 0<= adress <= Network.Constants.TPA_MAX
	 */
	public TPA(byte adress){
		int a = (int) (adress & (byte) Constants.TPA_MAX);
		
		if(a < 0 || a > Constants.TPA_MAX){
			throw new IllegalArgumentException("adress out of range "+a);
		}
		
		this.adress = a;
	}
	
	/**
	 * The integer representation of this adress
	 * @return the integer representation of this adress
	 * @ensures 0<= \result <= Network.Constants.TPA_MAX
	 */
	public int getAdress(){
		return adress;
	}
	
	/**
	 * The byte respresentation of this adress. The meaningfull bits will be in the 
	 * right most bits of the byte. The bits left of this will be 0
	 * @return the adress
	 * @ensure 0<= \result <= Network.Constants.TPA_MAX
	 */
	public byte getAdressAsByte(){
		return (byte) adress;
	}
	
	public boolean equals(TPA other){
		return this.adress == other.adress;
	}
	
	public String toString(){
		return Integer.toString(adress);
	}
	
}
